<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="平台银行账号">
          <el-select
            clearable
            v-model="form.sys_card_id"
            placeholder="平台银行账号"
          >
            <el-option
              v-for="item in sysCardsListData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户ID">
          <el-input clearable v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="merchant_id" label="商户ID" align="center" />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column prop="is_active" label="是否可用" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.is_active == 1 ? 'success' : 'danger'">
                {{ scope.row.is_active == 1 ? "可用" : "不可用" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="账户名称" align="center" />
          <el-table-column prop="bank" label="银行" align="center" />
          <el-table-column prop="card_name" label="收款人" align="center" />
          <el-table-column prop="card_number" label="账号" align="center" />
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column prop="swift_code" label="Swift码" align="center" />
          <el-table-column prop="remark" label="备注" align="center" />
          <el-table-column
            label="操作"
            align="center"
            width="120"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[15, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
      :sysCardsGroupData="sysCardsGroupData"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
//import { formatterTime } from "@/utils/utils";
import {
  sysBankCardSelectApi,
  sysMerchantCardsListApi,
  sysMerchantCardsUpdateApi,
} from "@/api/ExAssistant/settings";
import DialogForm from "./components/dialogForm.vue";

const dialogVisible = ref(false);
const btnLoading = ref(false);
const dialogTitle = ref("");
const loading = ref(false);
const tableData = ref([]);
const sysCardsListData = ref([]);
const sysCardsGroupData = ref([]);
const btnType = ref(null);
const formData = {
  sys_company_id: "",
  merchant_id: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  id: "",
  sys_card_ids: "",
  merchant_id: "",
  remark: "",
  is_active: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    name: form.name,
    merchant_id: form.merchant_id,
  };
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    const res = await sysMerchantCardsListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 获取系统卡列表
const getSysCardData = async () => {
  loading.value = true;
  try {
    const company = await sysBankCardSelectApi({});
    sysCardsListData.value = company.data.all;
    sysCardsGroupData.value = company.data.groups;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTableData();
};
// 查询
const onQuery = async () => {
  getTableData();
};
//新增
const onAdd = () => {
  dialogTitle.value = "新增商户充值账号";
  dialogVisible.value = true;
  Object.assign(dialogForm, { ...dialogFormData });
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    dialogTitle.value = "编辑商户充值账号";
    Object.assign(dialogForm, { ...row, sys_card_ids: [row.sys_card_id] });
    dialogVisible.value = true;
  }
};
// 提交(新增+编辑)
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await sysMerchantCardsUpdateApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTableData();
  } catch (error) {
    btnLoading.value = false;
    //        dialogVisible.value = false;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, { ...dialogFormData });
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTableData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTableData();
};
onMounted(() => {
  getTableData();
  getSysCardData();
});
</script>
